layui.define(function (exports) {
    var a = layui.jquery,
        f = layui.form,
        t = layui.table,
        ly = layui.layer,
        tid = 'form_table',

        T = {
            method: {
                //获取表格选中的id
                getCheckTableIds: function () {
                    var all = t.checkStatus(tid), ids = [];
                    if (all.length <= 0) return false;

                    all.data.forEach(function (v, i) {
                        ids.push(v.id);
                    });

                    return ids;
                },
                // +----------------------------------------------------------------------
                // | 自定义顶部按钮方法
                // +----------------------------------------------------------------------
                top: {
                    _checkView: function (json) {
                        window.open(json.url, '_blank');
                    },

                    _confirmSubmit: function (json) {
                        ly.confirm('确定要'+ json.title +'？', {title: '请确认'}, function () {
                            layui.listen.method.load();
                            a.post(json.url, function (data) {
                                ly.closeAll();
                                if (data.code === 1) {
                                    ly.msg(data.msg, {time: 1000}, function () {
                                        location.reload();
                                    });
                                } else {
                                    notify.error(data.msg);
                                }
                            })
                        });
                    },

                    _exportOrder: function (json) {
                        var ids = T.method.getCheckTableIds(), fVal = f.val("search-form");
                        fVal.tableExportIds = ids.length > 0 ? ids.join(',') : '';
                        layui.listen.method.load('处理数据中...');
                        a.get(json.url, fVal, function (data) {
                            ly.closeAll();
                            if (data.code === 1) {
                                ly.msg(data.msg, {time: 1000}, function () {
                                    window.open(data.data)
                                })
                            } else {
                                notify.error(data.msg);
                            }
                        })
                    },

                    _exportPower: function (json) {
                        var ids = T.method.getCheckTableIds(), fVal = f.val("search-form");
                        fVal.tableExportIds = ids.length > 0 ? ids.join(',') : '';
                        layui.listen.method.load('处理数据中...');
                        a.get(json.url, fVal, function (res) {
                            var timeStart = Date.now()
                            t.exportFile(res.title, res.data, {
                                type: 'xlsx', // 导出格式
                                title: res.name
                            });
                            var spent = (Date.now() - timeStart) / 1000
                            ly.closeAll();
                            notify.success('已导出，耗时 ' + spent + ' s');
                        });
                    },

                    _import: function (json) {
                        layui.upload.render({
                            elem: '.import-boost',
                            url: '/' + a('#admin_map').val() + '/uploads/uploadFile.html',
                            accept: 'file',
                            acceptMime: 'application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
                            exts: 'xls|xlsx',
                            before: function (obj) {
                                layui.listen.method.load('上传中...');
                            },
                            done: function (data) {
                                ly.closeAll();
                                if (data.code === 1) {
                                    layui.listen.method.load('上传成功，正在导入数据...');
                                    setTimeout(function () {
                                        a.post(json.url, {file: data.data.url}, function (data) {
                                            ly.closeAll();
                                            if (data.code === 1) {
                                                notify.success(data.msg, 1000, function () {
                                                    location.reload()
                                                });
                                            } else {
                                                notify.error(data.msg);
                                            }
                                        })
                                    }, 200);
                                }
                            },
                            error: function () {
                                ly.msg('上传失败')
                            }
                        });
                    }
                },
                // +----------------------------------------------------------------------
                // | 自定义右侧按钮方法
                // +----------------------------------------------------------------------
                right: {
                    /** 会员登录 */
                    login: function (data, tool, obj) {
                        ly.confirm('确定要登录吗？', {title: '请确认'}, function (index) {
                            layui.listen.method.load();
                            a.post(tool.url, {id: data.id, tableUniqueTitle: tool.unity_title}, function (data) {
                                ly.closeAll();
                                if (data.code === 1) {
                                    parent.layer.open({
                                        type: 2,
                                        area: ['390px', a(window).height() < 800 ? (90 + '%') : (800 + 'px')],
                                        anim: 2,
                                        offset: 'rb',
                                        shade: 0,
                                        title: '<a  href="' + data.data.url + '" target="_blank" >当前登录 《 ' + data.data.code + '》 右键可选择新窗口打开</a>',
                                        content: "" + data.data.url + "",
                                    });
                                } else {
                                    notify.error(data.msg);
                                }
                            })
                        });
                    },

                    deliver: function (data, tool, obj) {
                        var height = $(window).height();
                        var h = height < 720 ? '80%' : '720px';
                        ly.open({
                            type: 2,
                            title: '订单发货',
                            shadeClose: true,
                            shade: 0.8,
                            area: ['1000px', h],
                            content: tool.url + '&id=' + data.id,
                            btn: ['确认', '取消'],
                            btn2: function (index) {
                                ly.close(index);
                            },
                            btn1: function (index) {
                                var body = ly.getChildFrame('body', index);
                                layui.listen.method.load();
                                a.post(tool.url, body.find('.layui-form').serialize(), function (data) {
                                    ly.closeLast('dialog');
                                    if (data.code === 1) {
                                        ly.msg(data.msg, {time: 1000}, function () {
                                            ly.close(index);
                                            location.reload()
                                        });
                                    } else {
                                        ly.msg(data.msg);
                                    }
                                });
                                return false
                            },
                        });
                    }
                }
            },
        };

    exports('handle', T);
});